#include "tx_flash.h"

/*
    暂时hi用最后1k做数据保存
    刷除是按扇区
*/
void flash_erase_sector(u8 addr)
{
    FLASH_ADDR     = 0x3F;//15k 首地址
    FLASH_ADDR     = addr;
    FLASH_PASSWORD = FLASH_PASSWORD(0xB9);          // 写入操作密码
    FLASH_CON      = FLASH_SER_TRG(0x1);            // 触发扇区擦除
    
    while(!(FLASH_STA & FLASH_SER_FLG(0x1)));       // 等待扇区擦除空闲
}

void flash_program(u8 addr, u8 *p_data, u8 len)
{
    FLASH_ADDR = 0x3F;
    FLASH_ADDR = addr;
    
    while(len >= 1){
        while(!(FLASH_STA & FLASH_PROG_FLG(0x1)));  // 等待烧录空闲
        FLASH_DATA     = *(p_data++);
        FLASH_PASSWORD = FLASH_PASSWORD(0xB9);      // 写入操作密码
        FLASH_CON      = FLASH_PROG_TRG(0x1);       // 触发烧录

        len -= 1;
    }
}

void flash_read(u8 addr, u8 *p_data, u8 len)
{
    while(len != 0){
        *(p_data++) = *((u8 code *)(0x3F00 + addr++));
        len--;
    }
}


